<template>
  <div class="paySearch">
    <x-header>巡检录入
      <a href="javascript:;" class="save" @click="saveAction(type)">保存</a>
		  <a href="javascript:;" class="submit" @click="submitAction">提交</a>
    </x-header>
    <div class="paySearchContent">
      <div class="inspection">
        <div class="information-item equipment-item inspection-item">
          <img  class="information-item-leftImg" src="../../assets/img/securityCheckImg/titledecoration.png" alt="">
          <div class="information-item-title">表具信息</div>
        </div>
        <div class="information-item">
          <div class="information-item-title">出场编号</div>
          <popup-picker class="information-item-input" :data="list1" v-model="value1" @on-change="onChange" placeholder="请选择"></popup-picker>
          <!-- <img  class="information-item-rightImg" src="../../assets/img/securityCheckImg/rightArrow.png" alt=""> -->
        </div>
        <div class="information-item">
          <div class="information-item-title">上次示数</div>
          <input class="information-item-input" type="text" maxlength="16" placeholder="请录入" value="890">
        </div>
        <div class="information-item">
          <div class="information-item-title">本次示数</div>
          <input class="information-item-input" v-model="BEG_READ" type="text" maxlength="16" placeholder="请录入" value="">
          <img class="information-item-icon" src="../../assets/img/securityCheckImg/sao.png" alt="">
        </div>
        <div class="information-item">
          <div class="information-item-title">上次维修日期</div>
          <input class="information-item-input" type="text" maxlength="16" placeholder="请录入" value="890">
        </div>
        <div class="information-item">
          <div class="information-item-title">上次维修内容</div>
          <input class="information-item-input" type="text" maxlength="16" placeholder="请录入" value="890">
        </div>
        <div class="information-item equipment-item inspection-item">
          <img  class="information-item-leftImg" src="../../assets/img/securityCheckImg/titledecoration.png" alt="">
          <div class="information-item-title">巡检内容</div>
        </div>

        <div class="information-item" v-for="(item, index) in inspectionP_CODE" :key="index">
          <div class="information-item-title" data-value="item.CODE_VALUE">{{item.CODE_NAME}}</div>
          <input class="information-item-input" type="text" v-model="item.INPUT_VALUE" maxlength="16" placeholder="请录入" value="">
        </div>

        <div class="information-item equipment-item inspection-item">
          <img  class="information-item-leftImg" src="../../assets/img/securityCheckImg/titledecoration.png" alt="">
          <div class="information-item-title">铅封核对</div>
          <img class="information-item-icon inspection-item-icon" src="../../assets/img/securityCheckImg/add.png" alt="">
        </div>
        <div class="information-item inspection-popupPicker">
          <div class="popupPicker-item">
            <popup-picker class="popupPicker-item-popup" :data="list1" v-model="value1" @on-change="onChange" placeholder="请选择"></popup-picker>
            <!-- <img class="popupPicker-item-icon" src="../../assets/img/securityCheckImg/selectB.png" alt=""> -->
          </div>
          <input class="popupPicker-item-input" type="text" maxlength="16" placeholder="请录入" value="">
          <div class="popupPicker-item-image">
            <img class="popupPicker-item-saoIcon" src="../../assets/img/securityCheckImg/sao.png" alt="">
          <img class="popupPicker-item-delIcon" src="../../assets/img/securityCheckImg/deleteData.png" alt="">
          </div>
        </div>
        <div class="information-item inspection-popupPicker">
          <div class="popupPicker-item">
            <popup-picker class="popupPicker-item-popup" :data="list1" v-model="value1" @on-change="onChange" placeholder="请选择"></popup-picker>
            <!-- <img class="popupPicker-item-icon" src="../../assets/img/securityCheckImg/selectB.png" alt=""> -->
          </div>
          <input class="popupPicker-item-input" type="text" maxlength="16" placeholder="请录入" value="">
          <div class="popupPicker-item-image">
            <img class="popupPicker-item-saoIcon" src="../../assets/img/securityCheckImg/sao.png" alt="">
          <img class="popupPicker-item-delIcon" src="../../assets/img/securityCheckImg/deleteData.png" alt="">
          </div>
        </div>
        <div class="information-item inspection-popupPicker">
          <div class="popupPicker-item">
            <popup-picker class="popupPicker-item-popup" :data="list1" v-model="value1" @on-change="onChange" placeholder="请选择"></popup-picker>
            <!-- <img class="popupPicker-item-icon" src="../../assets/img/securityCheckImg/selectB.png" alt=""> -->
          </div>
          <input class="popupPicker-item-input" type="text" maxlength="16" placeholder="请录入" value="">
          <div class="popupPicker-item-image">
            <img class="popupPicker-item-saoIcon" src="../../assets/img/securityCheckImg/sao.png" alt="">
          <img class="popupPicker-item-delIcon" src="../../assets/img/securityCheckImg/deleteData.png" alt="">
          </div>
        </div>
        <div class="information-item inspection-popupPicker">
          <div class="popupPicker-item">
            <popup-picker class="popupPicker-item-popup" :data="list1" v-model="value1" @on-change="onChange" placeholder="请选择"></popup-picker>
            <!-- <img class="popupPicker-item-icon" src="../../assets/img/securityCheckImg/selectB.png" alt=""> -->
          </div>
          <input class="popupPicker-item-input" type="text" maxlength="16" placeholder="请录入" value="">
          <div class="popupPicker-item-image">
            <img class="popupPicker-item-saoIcon" src="../../assets/img/securityCheckImg/sao.png" alt="">
          <img class="popupPicker-item-delIcon" src="../../assets/img/securityCheckImg/deleteData.png" alt="">
          </div>
        </div>
        <div class="information-item inspection-popupPicker">
          <div class="popupPicker-item">
            <popup-picker class="popupPicker-item-popup" :data="list1" v-model="value1" @on-change="onChange" placeholder="请选择"></popup-picker>
            <!-- <img class="popupPicker-item-icon" src="../../assets/img/securityCheckImg/selectB.png" alt=""> -->
          </div>
          <input class="popupPicker-item-input" type="text" maxlength="16" placeholder="请录入" value="">
          <div class="popupPicker-item-image">
            <img class="popupPicker-item-saoIcon" src="../../assets/img/securityCheckImg/sao.png" alt="">
          <img class="popupPicker-item-delIcon" src="../../assets/img/securityCheckImg/deleteData.png" alt="">
          </div>
        </div>
        <div class="information-item equipment-item inspection-item">
          <img  class="information-item-leftImg" src="../../assets/img/securityCheckImg/titledecoration.png" alt="">
          <div class="information-item-title">巡检结果</div>
        </div>
        <div class="information-item">
          <div class="information-item-radio information-item-input inspection-item-radio">
            <label class="information-item-radio-y">
              <input type="radio" name="radio1" value="01" @change="bindRadio">
              <img class="information-item-radio-icon" src="../../assets/img/securityCheckImg/unselected.png" alt="" v-if="radioShowY">
              <img class="information-item-radio-icon" src="../../assets/img/securityCheckImg/selected.png" alt="" v-else>
              <div class="information-item-radio-text">正常</div>
            </label>
            <label class="information-item-radio-n">
              <input type="radio" name="radio1" value="02" @change="bindRadio">
              <img class="information-item-radio-icon" src="../../assets/img/securityCheckImg/unselected.png" alt="" v-if="radioShowN">
              <img class="information-item-radio-icon" src="../../assets/img/securityCheckImg/selected.png" alt="" v-else>
              <div class="information-item-radio-text">异常</div>
            </label>
          </div>
        </div>
        <div class="entryRecord-item">
          <div class="information-item-title entryRecord-title">存在问题</div>
          <textarea class="information-item-textarea" name="" id="" v-model="EXISTS_PROB" placeholder="请录入"></textarea>
        </div>
        <div class="entryRecord-item">
          <div class="information-item-title entryRecord-title">维护内容</div>
          <textarea class="information-item-textarea" name="" id="" v-model="FIX_CONTENT" placeholder="请录入"></textarea>
        </div>
        <div class="entryRecord-item">
          <div class="information-item-title entryRecord-title">巡检建议</div>
          <textarea class="information-item-textarea" name="" id="" v-model="INSP_ADVICE" placeholder="请录入"></textarea>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { XHeader, PopupPicker } from "vux";
// import inspection from "@/components/inspectionEntry/inspectionComponents"
import Vue from "vue";
import { getsn } from "@/utils/getsn";
import { UserInfo } from "@/utils/public";
import {
  sqlEntryInsert,
  sqlEntryUpdata,
  sqlEntryDelete,
  submiEntryTremoval
} from "@/utils/sqlEntry";
export default {
  data() {
    return {
      value1: ["居民"],
      title1: "手机机型",
      list1: [
        ["小米1", "iPhone1", "华为1", "情怀1", "三星1", "其他1", "不告诉你1"]
      ],
      radioShowY: true,
      radioShowN: true,
      radioList: null,
      inspectionP_CODE: null,
      BEG_READ: "", //本次示数
      EXISTS_PROB: "", //存在问题
      FIX_CONTENT: "", //维护内容
      INSP_ADVICE: "", //巡检建议
      INSP_RST: "", //巡检结果
      CONS_NO: "",
      M_CID: "",
      APP_NO:"",
      isExistence_type: 1,
      type: 1
    };
  },
  components: {
    XHeader,
    PopupPicker
  },
  methods: {
    bindRadio(e) {
      console.log(e.target.value);
      if (e.target.value == "02") {
        Vue.set(this, "radioShowY", true);
        Vue.set(this, "radioShowN", false);
      } else {
        Vue.set(this, "radioShowY", false);
        Vue.set(this, "radioShowN", true);
      }
      Vue.set(this, "INSP_RST", e.target.value);
    },
    onChange(val) {
      console.log("val change", val);
    },
    saveAction(type) {
      const _this = this;
      let M_CID = "";
      Vue.set(this, "type", type);
      if (this.isExistence_type == 2) {
        M_CID = this.M_CID;
      } else {
        M_CID = getsn();
      }
      console.log(2222222222222,this.APP_NO);
      const data = this.inspectionP_CODE.map(item => {
        return {
          DEV_INSP_DTL_ID: "",
          DEV_INSP_ID: "",
          ORG_NO: UserInfo.ORG_NO,
          INSP_ITEM: item.CODE_VALUE,
          INSP_RST: item.INPUT_VALUE,
          M_CID: M_CID
        };
      });
      const inspData = [
        {
          CONS_NO: this.CONS_NO,
          CHANGE_TYPE: type == 2 ? "3" : this.isExistence_type == 2 ? "2" : "1",
          DEV_INSP_ID: "",
          ORG_NO: UserInfo.ORG_NO,
          APP_NO: this.APP_NO,
          CONS_ID: "",
          GM_ID: "",
          ARRI_SITE_TIME: "",
          INSP_FNS_TIME: "",
          WRITE_TIME: "",
          INSP_ORG: "",
          INSP_DEPT: "",
          INSP_PER: "",
          END_READ: "",
          M_CID: M_CID,
          WORK_RST: "",
          RST_DESC: "",
          HOME_SITU: "",
          BEG_READ: this.BEG_READ,
          EXISTS_PROB: this.EXISTS_PROB, //存在问题
          FIX_CONTENT: this.FIX_CONTENT, //维护内容
          INSP_ADVICE: this.INSP_ADVICE, //巡检建议
          INSP_RST: this.INSP_RST //巡检结果
        }
      ];
      if (this.isExistence_type == 2) {
        sqlEntryUpdata("INSP_RST_INFO", M_CID, inspData, this.callback);
        sqlEntryDelete("INSP_DTL_INFO", M_CID, function(str, msg) {
          console.log(str, msg);
        });
        sqlEntryInsert("INSP_DTL_INFO", data, function(str, msg) {
          _this.$vux.toast.text("修改成功");
        });
      } else {
        sqlEntryInsert("INSP_RST_INFO", inspData, this.callback);
        sqlEntryInsert("INSP_DTL_INFO", data, this.callback);
      }
    },
    submitAction() {
      console.log("提交");
      const M_CID = getsn();
      console.log(2222222222222,this.APP_NO);
      const data = this.inspectionP_CODE.map(item => {
        return {
          DEV_INSP_DTL_ID: "",
          DEV_INSP_ID: "",
          ORG_NO: UserInfo.ORG_NO,
          INSP_ITEM: item.CODE_VALUE,
          INSP_RST: item.INPUT_VALUE,
          M_CID: M_CID
        };
      });
      const inspData = {
        INSP_RST_INFO: [
          {
            CONS_NO: this.CONS_NO,
            CHANGE_TYPE: "3",
            DEV_INSP_ID: "",
            ORG_NO: UserInfo.ORG_NO,
            APP_NO: this.APP_NO,
            CONS_ID: "",
            GM_ID: "",
            ARRI_SITE_TIME: "",
            INSP_FNS_TIME: "",
            WRITE_TIME: "",
            INSP_ORG: "",
            INSP_DEPT: "",
            INSP_PER: "",
            END_READ: "",
            M_CID: M_CID,
            WORK_RST: "",
            RST_DESC: "",
            HOME_SITU: "",
            BEG_READ: this.BEG_READ,
            EXISTS_PROB: this.EXISTS_PROB, //存在问题
            FIX_CONTENT: this.FIX_CONTENT, //维护内容
            INSP_ADVICE: this.INSP_ADVICE, //巡检建议
            INSP_RST: this.INSP_RST, //巡检结果
            INSP_DTL_INFO: data
          }
        ]
      };
      console.log(inspData);
      submiEntryTremoval(inspData,'RBP013',this.submitCallback);
    },
    callback(str, msg) {
      console.log(str, msg);
      if (msg.resultCode == "9999") {
        if (this.type == 2) {
          this.$vux.toast.text("提交成功");
        } else {
          if (str == "sqlUpdata") {
            this.$vux.toast.text("修改成功");
          } else {
            this.$vux.toast.text("添加成功");
          }
        }
      }
    },
    submitCallback(result) {
      console.log(result);
      this.saveAction(2)
    },
    getINSP_DTL_INFO(m_cid) {
      console.log(12312312312, m_cid);
      const _this = this;
      AndroidApi.queryTableData(
        JSON.stringify({
          whereClause: "select * from INSP_DTL_INFO where M_CID = ?",
          whereArgs: [m_cid]
        }),
        function(msg) {
          console.log(2222222222222222, msg);
          if (msg.resultCode === "9999") {
            if (msg.result.length > 0) {
              const data = msg.result.map(item => {
                for (let i in _this.inspectionP_CODE) {
                  if (_this.inspectionP_CODE[i].CODE_VALUE == item.INSP_ITEM) {
                    item.INSP_ITEM_NAME = _this.inspectionP_CODE[i].CODE_NAME;
                  }
                }
                return {
                  CODE_NAME: item.INSP_ITEM_NAME,
                  CODE_VALUE: item.INSP_ITEM,
                  INPUT_VALUE: item.INSP_RST
                };
              });

              Vue.set(_this, "inspectionP_CODE", data);
            }
          }
        }
      );
    }
  },
  mounted() {
    const _this = this;
    Vue.set(_this, "CONS_NO", localStorage.getItem("CONS_NO"));
    // Vue.set(_this, "APP_NO", localStorage.getItem("APP_NO"));
    Vue.set(_this, "APP_NO", '0118082958554654');
    // 查询表是否存在数据
    AndroidApi.queryTableData(
      JSON.stringify({
        whereClause:
          "select * from INSP_RST_INFO where CONS_NO= ? AND CHANGE_TYPE IN (1,2)",
        whereArgs: [_this.CONS_NO]
      }),
      function(msg) {
        console.log(111111111111111111111111, msg);
        if (msg.resultCode === "9999") {
          if (msg.result.length > 0) {
            _this.getINSP_DTL_INFO(msg.result[0].M_CID);
            Vue.set(_this, "M_CID", msg.result[0].M_CID);
            Vue.set(_this, "isExistence_type", 2);
            if (msg.result[0].INSP_RST == "02") {
              Vue.set(_this, "radioShowY", true);
              Vue.set(_this, "radioShowN", false);
            } else {
              Vue.set(_this, "radioShowY", false);
              Vue.set(_this, "radioShowN", true);
            }
            Vue.set(_this, "BEG_READ", msg.result[0].BEG_READ);
            Vue.set(_this, "EXISTS_PROB", msg.result[0].EXISTS_PROB);
            Vue.set(_this, "FIX_CONTENT", msg.result[0].FIX_CONTENT);
            Vue.set(_this, "INSP_ADVICE", msg.result[0].INSP_ADVICE);
          } else {
            Vue.set(_this, "isExistence_type", 1);
          }
        }
      }
    );

    // 巡检内容
    AndroidApi.queryTableData(
      '{"whereClause":"select * from P_CODE where code_type= ?","whereArgs":["mdmInspConfig"]}',
      function(msg) {
        if (msg.resultCode === "9999") {
          const data = msg.result.map(item => {
            return {
              CODE_NAME: item.CODE_NAME,
              CODE_VALUE: item.CODE_VALUE,
              INPUT_VALUE: ""
            };
          });
          Vue.set(_this, "inspectionP_CODE", data);
        }
      }
    );
    // 巡检结果
    AndroidApi.queryTableData(
      '{"whereClause":"select * from P_CODE where code_type= ?","whereArgs":["mdmInspRst"]}',
      function(msg) {
        if (msg.resultCode === "9999") {
          console.log(123123, msg);
          const data = msg.result.map(item => {
            return {
              CODE_NAME: item.CODE_NAME,
              CODE_VALUE: item.CODE_VALUE,
              radioShow: false
            };
          });
          Vue.set(_this, "radioList", data);
        }
      }
    );
  }
};
</script>
<style scoped lang="less">
@import "../../assets/less/style.less";
</style>
<style>
@import "../../assets/less/vuxStyle.less";
</style>
